<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>计算属性的set和get</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  </head>

  <body>
    <div id="app">
      <h1>{{fullName}}</h1>
    </div>

    <script>
      //创建Vue实例,得到 ViewModel
      var app = new Vue({
        el: "#app",
        data: {
          firstName: "kobe",
          lastName: "Bryant",
        },
        methods: {},
        computed: {
          fullName: {
            set: function (NewName) {
              var name = NewName.split(" ");
              (this.firstName = name[0]), (this.lastName = name[1]);
            },
            get: function () {
              return this.firstName + " " + this.lastName;
            },
          },
        },
      });
    </script>
  </body>
</html>
